package cn.tedu.shoot;

import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;

public class World extends JPanel{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 700;

    private Hero hero =new Hero();
    private FlyingObject[] enemies = {};
    private Bullet[] bullets = {};
    private Sky sky = new Sky();

    /**
     * 生成三种敌机对象
     * @return
     */
    public FlyingObject nextOne(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<5){
            return new Bee();
        }else if(type<13){
            return new Airplane();
        }else {
            return new BigAirplane();
        }
    }

    /**
     * 敌机入场
     */
    private int enterIndex = 0;
    public void enterAction(){
        enterIndex++;
        if(enterIndex%40==0){
            FlyingObject fo = nextOne();
            enemies = Arrays.copyOf(enemies,enemies.length+1);
            enemies[enemies.length-1] = fo;

            System.out.println(enemies.length);
            System.out.println(enterIndex);
        }
    }

    private int shootIndex = 0;
    public void shootAction(){
        shootIndex++;
        if (shootIndex%30==0){
            Bullet[] bs =hero.shoot();
            bullets =Arrays.copyOf(bullets,bullets.length+bs.length);
            System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
        }
    }

    public void moveAction(){
        sky.step();
        for (int i =0;i<enemies.length;i++){
            enemies[i].step();
        }
        for (int i =0;i<bullets.length;i++){
            bullets[i].step();
        }
    }

    public void outOfBoundsAction(){
        for (int i =0;i<enemies.length;i++){
            if(enemies[i].isOutBounds()){
                enemies[i] =enemies[enemies.length-1];
                enemies =Arrays.copyOf(enemies,enemies.length-1);
            }
        }
        for (int i=0;i<bullets.length;i++){
            if(bullets[i].isOutBounds()){
                bullets[i]=bullets[bullets.length-1];
                bullets=Arrays.copyOf(bullets,bullets.length-1);
            }
        }
    }


    private void action(){
        MouseAdapter m =new MouseAdapter() {
            public void mouseMoved(MouseEvent e){
                int x = e.getX();
                int y = e.getY();
                hero.moveTo(x,y);
            }
        };
        this.addMouseListener(m);
        this.addMouseMotionListener(m);

        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                enterAction();
                shootAction();
                moveAction();
                outOfBoundsAction();
                repaint();
            }
        },interval,interval);
    }

    public void paint(Graphics g){
        g.drawImage(sky.getImage(),sky.x,sky.y,null);
        g.drawImage(sky.getImage(),sky.x,sky.getY1(),null);
        g.drawImage(hero.getImage(),hero.x,hero.y,null);
        for (int i = 0; i < enemies.length; i++) {
            FlyingObject f = enemies[i];
            g.drawImage(f.getImage(),f.x,f.y,null);
        }
        for (int i =0; i<bullets.length;i++){
            Bullet b = bullets[i];
            g.drawImage(b.getImage(),b.x,b.y,null);
        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        World world = new World();
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH,HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        world.action();

    }
}
